<template>
  <vxe-input v-model="selectValue" placeholder="复选框的值" type="hidden" style="display: none"></vxe-input>
  <el-checkbox-group v-model="checkList" :clearable="clearable" :disabled="disabled" :readonly="readonly" @change="onChange">
    <el-checkbox v-for="item in list" :key="item.dictValue" :label="item.dictValue">{{ item.dictLabel }}</el-checkbox>
  </el-checkbox-group>
</template>
<script>
import { postRequest, getLocalStorage } from "@/api/mains";

export default defineComponent({
  name: "CommonRadio",
  props: {
    modelValue: {
      type: [Number, String],
      default: ""
    },
    opitionList: {
      type: Array,
      default: () => []
    },
    dictUrl: {
      type: String,
      default: ""
    },
    valToStr: {
      // 所选中的数组值转为字符串
      type: Boolean,
      default: false
    },
    clearable: {
      type: Boolean,
      default: true
    },
    dictCode: {
      type: String,
      default: ""
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  emits: {
    "update:model-value": null,
    change: null
  },

  setup(props, { emit }) {
    const checkList = ref([]);

    const selectValue = ref(props.modelValue);

    const list = ref([]);

    const getList = async () => {
      const { dictUrl, dictCode } = props;

      //  若传入url则按通过url获取数据
      if (dictUrl) {
        const { datas } = await postRequest(dictUrl);
        list.value = datas;
      } else if (dictCode) {
        list.value = getLocalStorage()[dictCode];
      }
    };

    getList();

    const onChange = value => {
      emit("update:model-value", JSON.parse(JSON.stringify(value)).join(","));
      emit("change", JSON.parse(JSON.stringify(value)).join(","));
    };

    watch(
      () => props.opitionList,
      newVal => {
        if (newVal && newVal.length) {
          list.value = newVal;
        }
      },
      {
        deep: true,
        immediate: true
      }
    );

    watch(
      () => props.modelValue,
      newVal => {
        if (newVal == null || newVal == "") {
          selectValue.value = "";
          checkList.value = [];
          return;
        }
        if (props.valToStr) {
          if (typeof newVal == "string") {
            selectValue.value = newVal;
            checkList.value = newVal.split(","); //[newVal];
          } else {
            selectValue.value = JSON.parse(JSON.stringify(newVal)).join(",");
            checkList.value = newVal;
          }
        } else {
          if (typeof newVal == "string") {
            selectValue.value = newVal;
            checkList.value = newVal.split(","); //[newVal];
          } else {
            selectValue.value = JSON.parse(JSON.stringify(newVal)).join(",");
            checkList.value = newVal;
          }
        }
      }
    );

    watch(
      () => props.dictCode,
      newVal => {
        if (newVal) {
          list.value = getLocalStorage()[newVal];
        }
      }
    );

    return {
      selectValue,
      checkList,
      list,
      onChange
    };
  }
});
</script>
